package com.xzh.controller;

import com.xzh.entity.User;
import com.xzh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class TestSsmController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getAllUser")
    public List<User> getAllUser(){
        return userService.getAllUser();
    }

    @RequestMapping("/getWebUser")
    public ModelAndView getWebUser(){
        ModelAndView mv=new ModelAndView();
        List<User> list=userService.getAllUser();
        mv.addObject("list",list);
        mv.setViewName("userlist");
        return mv;
    }


    @RequestMapping("/userReg")
    public ModelAndView userReg(User user){
        int a=userService.userReg(user);
        ModelAndView mv=new ModelAndView();
        if (a==1){
            mv.setViewName("login");
        }else {
            mv.setViewName("reg");
        }
        return mv;
    }

    @RequestMapping("/userJsonLogin")
    public User userJsonLogin(User user, HttpSession session){
        User u=userService.userLogin(user);
        session.setAttribute("user",u);
        return u;
    }
}
